﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_LeaseAgent_Detal_Operate:SqlInteractionBase
    {
        private DataEntity.T_LeaseAgent_Detal _objMessage = null;
        public override bool Insert(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_LeaseAgent_Detal_Insert]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_LeaseAgent_Detal)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidLeaseAgent_Detal_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidLeaseAgent_Detal_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_MainID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sLeaseAgent_Detal_Central", SqlDbType.NVarChar, 4000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_Central));
                cmdToExecute.Parameters.Add(new SqlParameter("@sLeaseAgent_Detal_Developer", SqlDbType.NVarChar, 4000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_Developer));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_LeaseAgent_Detal::Insert::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Update(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_LeaseAgent_Detal_Update]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_LeaseAgent_Detal)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidLeaseAgent_Detal_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidLeaseAgent_Detal_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_MainID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sLeaseAgent_Detal_Central", SqlDbType.NVarChar, 4000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_Central));
                cmdToExecute.Parameters.Add(new SqlParameter("@sLeaseAgent_Detal_Developer", SqlDbType.NVarChar, 4000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.LeaseAgent_Detal_Developer));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_LeaseAgent_Detal::Update::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public bool DeleteByMainID(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_LeaseAgent_Detal_Delete]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidLeaseAgent_Detal_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_LeaseAgent_Detal::Delete::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }

        public object SelectOneByMainID(string mainID)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_LeaseAgent_Detal_SelectOne]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataTable toReturn = new DataTable("t_LeaseAgent_Detal");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = new DataEntity.T_LeaseAgent_Detal();
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidLeaseAgent_Detal_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(mainID.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    this._objMessage.LeaseAgent_Detal_ID = (Guid)toReturn.Rows[0]["LeaseAgent_Detal_ID"];
                    this._objMessage.LeaseAgent_Detal_MainID = (Guid)toReturn.Rows[0]["LeaseAgent_Detal_MainID"];
                    this._objMessage.LeaseAgent_Detal_Central = toReturn.Rows[0]["LeaseAgent_Detal_Central"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["LeaseAgent_Detal_Central"];
                    this._objMessage.LeaseAgent_Detal_Developer = toReturn.Rows[0]["LeaseAgent_Detal_Developer"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["LeaseAgent_Detal_Developer"];
                }
                return _objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_LeaseAgent_Detal::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }

    }
}
